package cn.edu.xmu.server.controller;

import cn.edu.xmu.server.config.MinioConfig;
import cn.edu.xmu.server.config.ReturnNo;
import cn.edu.xmu.server.config.ReturnObject;
import cn.edu.xmu.server.controller.dto.FileUploadResult;
import cn.edu.xmu.server.controller.dto.TranslateResult;
import cn.edu.xmu.server.controller.vo.TranslateMessage;
import cn.edu.xmu.server.service.SendService;
import cn.edu.xmu.server.service.UploadService;
import cn.edu.xmu.server.util.MinioUtil;
import cn.edu.xmu.server.util.TranslateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.json.JSONObject;
import com.google.common.collect.ImmutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping(produces = "application/json;charset=UTF-8")
public class ImController {
    private final SendService sendService;
    private final UploadService uploadService;
    private static final String GROUP_CHAT_TOPIC = "IM_TOPIC";

    private final static Logger logger = LoggerFactory.getLogger(ImController.class);
    private final MinioUtil minioUtil;

    private final MinioConfig minioConfig;

    @Autowired
    public ImController(SendService sendService, UploadService uploadService, MinioUtil minioUtil, MinioConfig minioConfig) {
        this.sendService = sendService;
        this.uploadService = uploadService;
        this.minioUtil = minioUtil;
        this.minioConfig = minioConfig;
    }

    @PostMapping("/translate-message")
    public ReturnObject TranslateMessage(@RequestBody TranslateMessage message) {
        String result;
        logger.info("message:{}",message.toString());
        try {
            result = TranslateUtil.getTransResult(message.getMessage(), "auto", message.getTargetLanguage());
        } catch (Exception e) {
            return new ReturnObject(ReturnNo.ERROR);
        }
        return new ReturnObject(ReturnNo.OK, new TranslateResult(result));
    }


    @PostMapping("/file-upload")
    public ReturnObject upload(@RequestParam("file") MultipartFile file) {
        String objectName = this.minioUtil.upload(file);
        if (null != objectName) {
            return new ReturnObject(ReturnNo.OK,new FileUploadResult(minioConfig.getEndpoint() + "/" + minioConfig.getBucketName() + "/" + objectName));
        }
        return new ReturnObject(ReturnNo.ERROR);
    }

    @PostMapping("/multipart/init")
    public ReturnObject initMultiPartUpload(@RequestBody JSONObject requestParam) {
        // 路径
        String path = requestParam.getStr("path", "test");
        // 文件名
        String filename = requestParam.getStr("filename", "test.obj");
        // content-type
        String contentType = requestParam.getStr("contentType", "application/octet-stream");
        // md5-可进行秒传判断
        String md5 = requestParam.getStr("md5", "");
        // 分片数量
        Integer partCount = requestParam.getInt("partCount", 1);

        Map<String, Object> result = uploadService.initMultiPartUpload(path, filename, partCount, contentType);
        return new ReturnObject(result);
    }

//    @PostMapping("/multipart/upload")
//    public ReturnObject uploadPart(
//            @RequestParam("file") MultipartFile file,
//            @RequestParam("uploadId") String uploadId,
//            @RequestParam("partNumber") Integer partNumber,
//            @RequestParam("path") String path) {
//
//        String filename = file.getOriginalFilename();
//        String result = uploadService.uploadPart(file, uploadId, partNumber, path, filename);
//
//        return new ReturnObject(result);
//    }

    /**
     * 完成上传
     *
     * @param requestParam 用户参数
     * @return /
     */
    @PutMapping("/multipart/complete")
    public ReturnObject completeMultiPartUpload(
            @RequestBody JSONObject requestParam
    ) {
        System.out.println("complete");
        // 文件名完整路径
        String objectName = requestParam.getStr("objectName");
        String uploadId = requestParam.getStr("uploadId");
        Assert.notNull(objectName, "objectName must not be null");
        Assert.notNull(uploadId, "uploadId must not be null");
        boolean result = uploadService.mergeMultipartUpload(objectName, uploadId);
        String finalUrl = minioUtil.preview(objectName);
        System.out.println("complete-finish");
        return new ReturnObject(ImmutableMap.of("success", result, "url", finalUrl));
    }



}
